//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
	.global __alloca

	__alloca:
	    sub sp, sp, r0
	    mov r0, sp
	    mov pc, lr

//============================================================
//           div routine
//============================================================
    .global  ___umodsi3
    ___umodsi3:
        mov r2, r0
        mov r0, r1
        mov r1, r2
        b   x_uremainder

    .global  ___udivsi3
    ___udivsi3:
        mov r2, r0
        mov r0, r1
        mov r1, r2
        b   x_udivide

    .global  ___modsi3
    ___modsi3:
        mov r2, r0
        mov r0, r1
        mov r1, r2
        b   x_remainder

    .global     ___divsi3
    ___divsi3:
        mov r2, r0
        mov r0, r1
        mov r1, r2
        b   x_divide

    .global     _x_divtest
    .global     x_divtest
    _x_divtest:
    x_divtest:
        mov pc,lr

    .global     _x_remainder
    .global     x_remainder
    _x_remainder:
    x_remainder:
        stmfd   sp!,{lr}
        bl      x_divide
        mov     r0,r1
        ldmfd   sp!,{pc}

    .global     _x_uremainder
    .global     x_uremainder
    _x_uremainder:
    x_uremainder:
        stmfd   sp!,{lr}
        bl      x_udivide
        mov     r0,r1
        ldmfd   sp!,{pc}

    x_overflow:
        mvn     r0,#0
        mov     pc,lr

    .global     _x_udivide
    .global     x_udivide       ; /* r0 = r1 / r0; r1 = r1 % r0 */
    _x_udivide:
    x_udivide:
        cmp     r0, #1
        bcc     x_overflow
        beq     x_divide_l0
        mov     ip, #0
        movs    r1,r1
        bpl     x_divide_l1
        orr     ip,ip,#0x20000000   ; /* ip bit 0x20000000 = -ve r1 */
        movs    r1,r1,lsr #1
        orrcs   ip,ip,#0x10000000   ; /* ip bit 0x10000000 = bit 0 of r1 */
        b       x_divide_l1

    x_divide_l0:            ; /* r0 == 1 */
        mov     r0,r1
        mov     r1,#0
        mov     pc,lr

    .global     _x_divide
    .global     x_divide        ; /* r0 = r1 / r0; r1 = r1 % r0 */
    _x_divide:
    x_divide:
        cmp     r0,#1
        bcc     x_overflow
        beq     x_divide_l0
        ands    ip,r0,#0x80000000
        rsbmi   r0,r0,#0
        ands    r2,r1,#0x80000000
        eor     ip,ip,r2
        rsbmi   r1,r1,#0
        orr     ip,r2,ip,lsr #1 ; /* ip bit 0x40000000 = -ve division */
                                ; /* ip bit 0x80000000 = -ve remainder */

    x_divide_l1:
        mov     r2,#1
        mov     r3,#0

        cmp     r1,r0
        bcc     x_divide_b0
        cmp     r1,r0,lsl #1
        bcc     x_divide_b1
        cmp     r1,r0,lsl #2
        bcc     x_divide_b2
        cmp     r1,r0,lsl #3
        bcc     x_divide_b3
        cmp     r1,r0,lsl #4
        bcc     x_divide_b4
        cmp     r1,r0,lsl #5
        bcc     x_divide_b5
        cmp     r1,r0,lsl #6
        bcc     x_divide_b6
        cmp     r1,r0,lsl #7
        bcc     x_divide_b7
        cmp     r1,r0,lsl #8
        bcc     x_divide_b8
        cmp     r1,r0,lsl #9
        bcc     x_divide_b9
        cmp     r1,r0,lsl #10
        bcc     x_divide_b10
        cmp     r1,r0,lsl #11
        bcc     x_divide_b11
        cmp     r1,r0,lsl #12
        bcc     x_divide_b12
        cmp     r1,r0,lsl #13
        bcc     x_divide_b13
        cmp     r1,r0,lsl #14
        bcc     x_divide_b14
        cmp     r1,r0,lsl #15
        bcc     x_divide_b15
        cmp     r1,r0,lsl #16
        bcc     x_divide_b16
        cmp     r1,r0,lsl #17
        bcc     x_divide_b17
        cmp     r1,r0,lsl #18
        bcc     x_divide_b18
        cmp     r1,r0,lsl #19
        bcc     x_divide_b19
        cmp     r1,r0,lsl #20
        bcc     x_divide_b20
        cmp     r1,r0,lsl #21
        bcc     x_divide_b21
        cmp     r1,r0,lsl #22
        bcc     x_divide_b22
        cmp     r1,r0,lsl #23
        bcc     x_divide_b23
        cmp     r1,r0,lsl #24
        bcc     x_divide_b24
        cmp     r1,r0,lsl #25
        bcc     x_divide_b25
        cmp     r1,r0,lsl #26
        bcc     x_divide_b26
        cmp     r1,r0,lsl #27
        bcc     x_divide_b27
        cmp     r1,r0,lsl #28
        bcc     x_divide_b28
        cmp     r1,r0,lsl #29
        bcc     x_divide_b29
        cmp     r1,r0,lsl #30
        bcc     x_divide_b30
        cmp     r1,r0,lsl #31
        subhs   r1,r1,r0,lsl #31
        addhs   r3,r3,r2,lsl #31
        cmp     r1,r0,lsl #30
        subhs   r1,r1,r0,lsl #30
        addhs   r3,r3,r2,lsl #30
    x_divide_b30:
        cmp     r1,r0,lsl #29
        subhs   r1,r1,r0,lsl #29
        addhs   r3,r3,r2,lsl #29
    x_divide_b29:
        cmp     r1,r0,lsl #28
        subhs   r1,r1,r0,lsl #28
        addhs   r3,r3,r2,lsl #28
    x_divide_b28:
        cmp     r1,r0,lsl #27
        subhss  r1,r1,r0,lsl #27
        addhs   r3,r3,r2,lsl #27
    x_divide_b27:
        cmp     r1,r0,lsl #26
        subhs   r1,r1,r0,lsl #26
        addhs   r3,r3,r2,lsl #26
    x_divide_b26:
        cmp     r1,r0,lsl #25
        subhs   r1,r1,r0,lsl #25
        addhs   r3,r3,r2,lsl #25
    x_divide_b25:
        cmp     r1,r0,lsl #24
        subhs   r1,r1,r0,lsl #24
        addhs   r3,r3,r2,lsl #24
    x_divide_b24:
        cmp     r1,r0,lsl #23
        subhs   r1,r1,r0,lsl #23
        addhs   r3,r3,r2,lsl #23
    x_divide_b23:
        cmp     r1,r0,lsl #22
        subhs   r1,r1,r0,lsl #22
        addhs   r3,r3,r2,lsl #22
    x_divide_b22:
        cmp     r1,r0,lsl #21
        subhs   r1,r1,r0,lsl #21
        addhs   r3,r3,r2,lsl #21
    x_divide_b21:
        cmp     r1,r0,lsl #20
        subhs   r1,r1,r0,lsl #20
        addhs   r3,r3,r2,lsl #20
    x_divide_b20:
        cmp     r1,r0,lsl #19
        subhs   r1,r1,r0,lsl #19
        addhs   r3,r3,r2,lsl #19
    x_divide_b19:
        cmp     r1,r0,lsl #18
        subhs   r1,r1,r0,lsl #18
        addhs   r3,r3,r2,lsl #18
    x_divide_b18:
        cmp     r1,r0,lsl #17
        subhs   r1,r1,r0,lsl #17
        addhs   r3,r3,r2,lsl #17
    x_divide_b17:
        cmp     r1,r0,lsl #16
        subhs   r1,r1,r0,lsl #16
        addhs   r3,r3,r2,lsl #16
    x_divide_b16:
        cmp     r1,r0,lsl #15
        subhs   r1,r1,r0,lsl #15
        addhs   r3,r3,r2,lsl #15
    x_divide_b15:
        cmp     r1,r0,lsl #14
        subhs   r1,r1,r0,lsl #14
        addhs   r3,r3,r2,lsl #14
    x_divide_b14:
        cmp     r1,r0,lsl #13
        subhs   r1,r1,r0,lsl #13
        addhs   r3,r3,r2,lsl #13
    x_divide_b13:
        cmp     r1,r0,lsl #12
        subhs   r1,r1,r0,lsl #12
        addhs   r3,r3,r2,lsl #12
    x_divide_b12:
        cmp     r1,r0,lsl #11
        subhs   r1,r1,r0,lsl #11
        addhs   r3,r3,r2,lsl #11
    x_divide_b11:
        cmp     r1,r0,lsl #10
        subhs   r1,r1,r0,lsl #10
        addhs   r3,r3,r2,lsl #10
    x_divide_b10:
        cmp     r1,r0,lsl #9
        subhs   r1,r1,r0,lsl #9
        addhs   r3,r3,r2,lsl #9
    x_divide_b9:
        cmp     r1,r0,lsl #8
        subhs   r1,r1,r0,lsl #8
        addhs   r3,r3,r2,lsl #8
    x_divide_b8:
        cmp     r1,r0,lsl #7
        subhs   r1,r1,r0,lsl #7
        addhs   r3,r3,r2,lsl #7
    x_divide_b7:
        cmp     r1,r0,lsl #6
        subhs   r1,r1,r0,lsl #6
        addhs   r3,r3,r2,lsl #6
    x_divide_b6:
        cmp     r1,r0,lsl #5
        subhs   r1,r1,r0,lsl #5
        addhs   r3,r3,r2,lsl #5
    x_divide_b5:
        cmp     r1,r0,lsl #4
        subhs   r1,r1,r0,lsl #4
        addhs   r3,r3,r2,lsl #4
    x_divide_b4:
        cmp     r1,r0,lsl #3
        subhs   r1,r1,r0,lsl #3
        addhs   r3,r3,r2,lsl #3
    x_divide_b3:
        cmp     r1,r0,lsl #2
        subhs   r1,r1,r0,lsl #2
        addhs   r3,r3,r2,lsl #2
    x_divide_b2:
        cmp     r1,r0,lsl #1
        subhs   r1,r1,r0,lsl #1
        addhs   r3,r3,r2,lsl #1
    x_divide_b1:
        cmp r1,r0
        subhs   r1,r1,r0
        addhs   r3,r3,r2
    x_divide_b0:
        tst     ip,#0x20000000
        bne     x_udivide_l1
        mov     r0,r3
        cmp     ip,#0
        rsbmi   r1,r1,#0
        movs    ip,ip,lsl #1
        rsbmi   r0,r0,#0
        mov     pc,lr

    x_udivide_l1:
        tst     ip,#0x10000000
        mov     r1,r1,lsl #1
        orrne   r1,r1,#1
        mov     r3,r3,lsl #1
        cmp     r1,r0
        subhs   r1,r1,r0
        addhs   r3,r3,r2
        mov     r0,r3
        mov     pc,lr

//	.global ___gccmain
//    ___gccmain:
//	    mov pc,lr
